<?php

/**
 * IndexController - The default controller class
 * 
 * @author
 * @version 
 */

require_once 'Zend/Controller/Action.php';

class Adminb_OrderController extends App_Zend_Controller_Action_Adminb
{
	

	public function init(){
		parent::init();
		$member=new CMember();
		
		if(!$member->isUser($this->_request->getModuleName(),$this->_request->getControllerName())){

			
				$this->_redirect(MY_URL.'/auth-login?preurl='.CURRENT_URL);

			
		}
		
	}
	
	public function preDispatch()
	{

		$this->getHelper ('layout')->setLayout('framelayout');
		
		$this->view->addHelperPath(MY_ROOT.'/vhelpers');
		
		Zend_Controller_Action_HelperBroker::addPath(MY_ROOT.'/chelpers');
		
		$this->view->assign('currentAction',$this->_request->getActionName());
			
	}

    public function indexAction() 
    {

    	$orderfl=new CFenlei('order');
    	
    	//$fenleiArray=$orderfl->getAllFenlei();
    	
    	$fenleiArray = array();
    	$fenleiArray[] = Array ('flid'=>1, 'pid'=>0, 'flname'=>'未付款',  'en_flname'=>'Payment Pending', 'ordering'=>0); 
    	$fenleiArray[] = Array ('flid'=>2, 'pid'=>0, 'flname'=>'待确认',  'en_flname'=>'Paid', 'ordering'=>0);
    	$fenleiArray[] = Array ('flid'=>3, 'pid'=>0, 'flname'=>'待发货',  'en_flname'=>'In Delivery', 'ordering'=>0);
    	$fenleiArray[] = Array ('flid'=>4, 'pid'=>0, 'flname'=>'已发货 ',  'en_flname'=>'Sent', 'ordering'=>0);
    	$fenleiArray[] = Array ('flid'=>6, 'pid'=>0, 'flname'=>'纠纷 ',  'en_flname'=>'Dispute', 'ordering'=>0);
    	$fenleiArray[] = Array ('flid'=>7, 'pid'=>0, 'flname'=>'正常取消 ',  'en_flname'=>'Canceled', 'ordering'=>0);
    	$fenleiArray[] = Array ('flid'=>9, 'pid'=>0, 'flname'=>'已完成 ',  'en_flname'=>'Completed', 'ordering'=>0);
    	
    	$orderfl->fenleiLang='en_flname';    	
		
		$orderfl->getAllNew($fenleiArray,0);
		
		$fenleiString=str_replace(array('<div class="smallChildDiv"></div>','<div></div>'),array('',''),$orderfl->fenleiString);

		$this->view->assign('fenleiString',$fenleiString);
		
		$this->view->assign('fenleiArray',$fenleiArray);		

    }
    
    public function getitemajaxAction(){
    	
    	$this->getHelper ('layout')->disableLayout ();
    	
    	$flid=$this->_request->getParam('flid',0);
    	
    	$page=$this->_request->getParam('page',1);
    	
    	$searchQuery=$this->_request->getParam('searchQuery','');
    	
    	$searchArray=json_decode(stripslashes($searchQuery),true);
    	
    	$order=new COrder();
		
    	$where=array();
    	
    	if($flid==0){
    		
    		//$where[]=R_PRE."orders.uid='".$_COOKIE[COOKIE_PRE.'uid']."' AND ".R_PRE."orders.flid<>'2'";
    		$where[]=R_PRE."orders.uid='".$_COOKIE[COOKIE_PRE.'uid']."'";
    		
    	}else{
    		
    		$where[]=R_PRE."orders.uid='".$_COOKIE[COOKIE_PRE.'uid']."' AND ".R_PRE."orders.flid='$flid'";
    		
    	}
    	
    	if($searchArray['orderid']!=''){
    		
    		$where[]="orderid='".$searchArray['orderid']."'";
    		
    	}
    	
    	if($searchArray['time_from']!=''){
    		
    		$searchArray['time_from']=strtotime($searchArray['time_from']);
    		
    		$where[]="add_time>'".$searchArray['time_from']."'";
    		
    	}
    	
    	if($searchArray['time_to']!=''){
    		
    		$searchArray['time_to']=strtotime($searchArray['time_to']);
    		
    		$where[]="add_time<'".$searchArray['time_to']."'";
    		
    	}
    	
    	if($searchArray['buyer']!=''){
    		
    		$member=new CMember();
    		
    		$userArray=$member->getUserDetails(array('username'=>$searchArray['buyer']));
    		
    		$where[]=R_PRE."orders.uid='".$userArray[0]['uid']."'";
    		
    	}
    	
    	if($searchArray['seller']!=''){
    		
    		$member=new CMember();
    		
    		$userArray=$member->getUserDetails(array('username'=>$searchArray['seller']));
    		
    		$where[]="seller_uid='".$userArray[0]['uid']."'";
    		
    	}
    	
    	if($searchArray['money_from']!=''){
    		
    		$where[]="money>'".$searchArray['money_from']."'";
    		
    	}
    	
    	if($searchArray['money_to']!=''){
    		
    		$where[]="money<'".$searchArray['money_to']."'";
    		
    	}
    	
    	if($searchArray['flid2']!='0'&&$searchArray['flid2']!=''){
    		
    		$where[]=R_PRE."orders.flid='".$searchArray['flid2']."'";
    		
    	}
    	
    	$where=join(' AND ',$where);
    	
		$orderCounts=$order->getAllOrderCounts(0,$where);
		
		Zend_Loader::loadClass("CFilter");
    	
    	$pageArray = CFilter::filterPage ( $page, $orderCounts, 10);
		
		$orderArray=$order->getAllOrders($pageArray['from'],10,0,$where);
		
		foreach ($orderArray as $v) {
			if ($v['flid'] == 1 && (time() - $v['add_time'] > 172800)) { //未付款超过48个小时
				$order->editOrder($v['orderid'], array('flid'=>7));
			}
		}
		
		$orderArray=$order->getAllOrders($pageArray['from'],10,0,$where);
		
		Zend_Loader::loadClass("CPage");
    	
    	$pageString = CPage::commonDivAjax($pageArray ['pageNumber'], $pageArray ['page'], 'productEffection.showAll',$flid); //生成分页字符串
    	
    	$this->view->assign ( "pageString", $pageString );
    	
    	$this->view->assign ( "pageArray", $pageArray );

		$this->view->assign ( 'orderArray', $orderArray );
    	
    }
    
    public function docancelAction(){
    	
    	$this->_helper->viewRenderer->setNoRender();
		
		$orderid=$this->_request->getParam('orderid');

		$order=new COrder();
		
		$order->editOrder($orderid,array('flid'=>7));
		
		$account=new CAccount();
				
		$account->delAccountBySign($orderid);
		
		$this->view->showMsg("Succeed to cancel #PO$orderid!");
    	
    }
	
    public function moreAction(){
    	
    	$orderid=$this->_request->getParam('orderid');
    	
    	$order=new COrder();
    	
    	$orderArray=$order->getDetail($orderid);
    	
    	$this->view->assign('orderArray',$orderArray);
    	
    	$itemArray=array();
    	
    	//eval('$itemArray='.$orderArray[0]['order_string'].';');
    	
    	$itemArray=unserialize($orderArray[0]['order_string']);
    	
    	$this->view->assign('itemArray',$itemArray);
    	
    	$attachment=new COrderAttachment();
    	
    	$attachmentArray=$attachment->getAttachments($orderid);
    	
    	$this->view->assign('attachmentArray',$attachmentArray);
    	
    	$attachment->setIsSeller(0);
    	
    	$attachmentArray2=$attachment->getAttachments($orderid);
    	
    	$this->view->assign('attachmentArray2',$attachmentArray2);
    	
    	$orderid_string=serialize(array($orderid));
    	
    	$account=new CAccount();
	   		
   		$reimburseAccountArray=$account->getAccountDetails(array('orderid_string'=>$orderid_string,'flid'=>'5'));
   		
   		$this->view->assign('reimburseAccountArray',$reimburseAccountArray);
   		
   		$orderRemark=new COrderRemark();
   		
   		$disputeRemarkArray=$orderRemark->getRemarkDetails(array('orderid'=>$orderid,'flid'=>'1'));
   		
   		$this->view->assign('disputeRemarkArray',$disputeRemarkArray);
   		
   		$reimburseRemarkArray=$orderRemark->getRemarkDetails(array('orderid'=>$orderid,'flid'=>'2'));
   		
   		$this->view->assign('reimburseRemarkArray',$reimburseRemarkArray);
   		
   		//$account->getAccountDetails(array('orderid_string'=>serialize(array($orderid)),'flid'=>'5'));
   		
   		$cancelDisputeRemarkArray=$orderRemark->getRemarkDetails(array('orderid'=>$orderid,'flid'=>'3'));
   		
   		$this->view->assign('cancelDisputeRemarkArray',$cancelDisputeRemarkArray);
   		
   		$cancelReimbursementArray=$orderRemark->getRemarkDetails(array('orderid'=>$orderid,'flid'=>'4'));
   		
   		$this->view->assign('cancelReimbursementArray',$cancelReimbursementArray);
   		
   		$orderItem=new COrderItem();
   		
   		$orderItemArray=$orderItem->getDetail($orderid,$itemArray[0]['itemid']);
   		
   		$this->view->assign('orderItemArray',$orderItemArray);
   		
   		$orderDelay=new COrderDelay();
    	
    	$orderDelayArray=$orderDelay->getDelayDetails(array('orderid'=>$orderid));

    	$this->view->assign('orderDelayArray',$orderDelayArray);
 
		$productReview = new CProductreview();
		$this->view->productReview = $productReview;		
		$contact = new COrderContact();
		$this->view->contact = $contact->find($orderArray[0]['contactid'])->current();		
    }
    
    public function expressAction(){
    	
    	$orderid=$this->_request->getParam('orderid');
    	
    	$itemid=$this->_request->getParam('itemid');
    	
    	$shipping_cost=$this->_request->getParam('shipping_cost');
    	
    	$this->view->assign('shipping_cost',$shipping_cost);
    	
    	$orderItem=new COrderItem();
    	
    	$orderItemArray=$orderItem->getDetail($orderid,$itemid);
    	
    	$this->view->assign('orderItemArray',$orderItemArray);
    	
    	if(count($orderItemArray)>0){
    	
    		$orderContact=new COrderContact();
    	
    		$buyerContactArray=$orderContact->getContactByUid($orderItemArray[0]['uid']);
    	
    		$this->view->assign('buyerContactArray',$buyerContactArray);
    	
    		$this->view->assign('orderid',$orderid);
    		
    		$this->view->assign('itemid',$itemid);
    	
    	}else{
    		
    		$this->_helper->viewRenderer->setNoRender();
    		
    		echo $this->view->getMsg('No shipping information!');
    		
    	}
    	
    	
    }
    
    public function satisfiedAction(){
    	
    	$this->_helper->viewRenderer->setNoRender();
    	
    	$orderid=$this->_request->getParam('orderid');
    	
    	$isSatisfied=$this->_request->getParam('isSatisfied');
    	
    	$order=new COrder();
    	
    	$orderArray=$order->getDetail($orderid);
    	
    	$seller=new CSeller();
    	
    	if($isSatisfied=='1'){
    	
    		$order->editOrder($orderid,array('flid'=>5));

    		$seller->addGoodCredit($orderArray[0]['seller_uid']);
    		
    		$buyer=new CBuyer();
    		
    		$buyer->addGoodCredit($_COOKIE[COOKIE_PRE.'uid']);
    		

    	
    	}elseif($isSatisfied=='0'){
    		
    		$order->editOrder($orderid,array('flid'=>6));

    		$seller->addBadCredit($orderArray[0]['seller_uid']);
    		
    	}
    	
    	$this->view->showMsg('Thanks for confirmation!',$_SERVER['HTTP_REFERER']);
    	
    }
    
    public function getaccountAction(){
    	
    	$this->getHelper ('layout')->disableLayout ();
    	
    	$member=new CMember();
    	
    	$memberArray=$member->getUserDetails(array('uid'=>$_COOKIE[COOKIE_PRE.'uid']));
    	
    	$this->view->assign('memberArray',$memberArray);
    	
    }
    
    public function orderremarkAction(){
    	
    	$this->getHelper ('layout')->disableLayout ();
    	
    	$orderid=$this->_request->getParam('orderid');
    	
    	$flid=$this->_request->getParam('flid');
    	
    	$orderflid=$this->_request->getParam('orderflid');
    	
    	$message=$this->_request->getParam('message');
    	
    	$this->view->assign('orderid',$orderid);
    	
    	$this->view->assign('flid',$flid);
    	
    	$this->view->assign('orderflid',$orderflid);
    	
    	$this->view->assign('message',$message);
    	
    }
    
    public function doorderremarkAction(){    	
    	
    	
    	$thisArray=$this->_request->getParams();
    	
		$filters = array(
    		'*'   => 'StringTrim'
		);

		$validators = array(
		);
		
		$input = new Zend_Filter_Input($filters, $validators, $thisArray);
		
		$orderRemark=new COrderRemark();
		
		$orderRemarkArray=$orderRemark->getRemarkDetails(array('orderid'=>$input->orderid,'flid'=>$input->flid));
		
		$data=array(
					'orderid'=>$input->orderid,
					'content'=>$input->content,
					'flid'=>$input->flid,
					'add_time'=>time()
					);
					
		if(count($orderRemarkArray)>0){
			
			$orderRemark->editRemark($orderRemarkArray[0]['remarkid'],array('content'=>$input->content,'add_time'=>time()));
			
		}else{
			
			$orderRemark->createOrderRemark($data);
			
		}
		
		if(is_numeric($input->orderflid)){
		
			$order=new COrder();
			
			$order->editOrder($input->orderid,array('flid'=>$input->orderflid));
		
		}
		
		$member=new CMember();
		
		$memberArray=$member->getUserDetails(array('uid'=>$_COOKIE[COOKIE_PRE.'uid']));
		
		$fenlei=new CFenlei('order_remark');
		
		$remarkContent=$fenlei->getFenLeiNameByFlid($input->flid);
		
		$input->content=$input->content.'<br /><a href="administrator-order-more?orderid='.$input->orderid.'">查看订单</a>';
		
		$data=array(
    				'from_uid'=>$_COOKIE[COOKIE_PRE.'uid'],
    				'to_uid'=>1,
    				'add_time'=>time(),
    				'subject'=>'会员'.$memberArray[0]['username'].'对订单#'.$input->orderid.'进行操作',
    				'content'=>$remarkContent.':'.$input->content,
    				'has_read'=>0,
					'from_has_del'=>'1',
    				'flid'=>'2',
    			   );
    			   
    	$pm=new CPm();
    	
    	$pm->createPm($data);
    	
		
		$this->view->msg = 'Succeed to confirm!';
    	
    }
    
    public function doapproveAction(){
    	
    	$this->_helper->viewRenderer->setNoRender();
    	
    	$has_approved=$this->_request->getParam('has_approved');
    	
    	$orderid=$this->_request->getParam('orderid');
    	
    	$orderDelay=new COrderDelay();
    	
    	$data=array('has_approved'=>$has_approved);
    	
    	$orderDelay->editDelay($orderid,$data);
    	
    	$order=new COrder();
    		
    	$orderDelayArray=$orderDelay->getDelayDetails(array('orderid'=>$orderid));
    	
    	if($has_approved=='1'){
    		
    		$order->updateDeliveryDay($orderDelayArray[0]['delay_days'],$orderid,true);
    		
    	}
    	
    	$this->view->showMsg('Success!',$_SERVER['HTTP_REFERER']);
    	
    }
    
    public function canceldisputeAction(){
    	
    	$this->_helper->viewRenderer->setNoRender();
    	
    	$orderid=$this->_request->getParam('orderid');
    	
    	$order=new COrder();
    	
    	$orderArray=$order->getDetail($orderid);
    	
    	if($orderArray[0]['flid']=='6'){
    		
    		if($orderArray[0]['ship_time']=='0'){
    		
    			$order->editOrder($orderid,array('flid'=>1));
    		
    		}else{
    			
    			$order->editOrder($orderid,array('flid'=>1));
    			
    		}
    		
    	}
    	
    }
    
    public function addreviewAction()
    {
    	$itemId = $this->getRequest()->getParam('itemid');
    	$orderId = $this->getRequest()->getParam('orderid');
    	$uid = $_COOKIE[COOKIE_PRE . 'uid'];
    	
    	$itemId = intval($itemId);
    	$orderId = intval($orderId);
    	
    	$productReview = new CProductreview();
    	
    	$this->view->itemId = $itemId;
    	$this->view->orderId = $orderId;
    	
    	$row = $productReview->isExist($itemId, $uid, $orderId);
    	if (count($row) > 0) {
    		echo '<script>alert("You have review");parent.addComment.shutKuang();</script>'; 
    		exit();   		
    	}
    	
    	if ($this->getRequest()->isPost()) {
    		$productScore = $this->getRequest()->getParam('product_score');
    		$shipScore = $this->getRequest()->getParam('ship_score');
    		$overallScore = $this->getRequest()->getParam('overall_score');
    		$comment = $this->getRequest()->getParam('comment');
    		$comment = $this->view->escape($comment);
    		
    		$row = array('itemid'=>$itemId, 'orderid'=>$orderId, 'uid'=>$_COOKIE[COOKIE_PRE . 'uid'],
    					 'product_score'=>$productScore, 'ship_score'=>$shipScore, 'overall_score'=>$overallScore, 'comments'=>$comment, 'add_time'=>time());
    		
    		if ($productReview->add($row)) {
    			echo '<script>parent.GGSHOP.loading.show();parent.$("#admins_frame").attr("src", "/adminb-order-more?orderid='.$orderId.'");parent.GGSHOP.loading.hidden();parent.addComment.shutKuang();</script>';
    		}
    	}
    	
    }
    
	public function postAction(){
	
		//$this->_helper->viewRenderer->setNoRender();
		
		//$this->getHelper ('layout')->disableLayout ();
	
	}
    

    
}
